﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Sale
{    
    public partial class XiaoShou_FuKuan_In_Search : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String sql = @"SELECT     dbo.销售到款记录.ID, 
                                      dbo.销售到款记录.收款单号, 
                                      dbo.产品销售合同.合同编号, 
                                      dbo.产品订货单位.名称, 
                                      dbo.产品销售合同.金额, 
                                      dbo.销售到款记录.收款, 
                                      dbo.销售到款记录.付款日期
                         FROM         dbo.产品销售合同 
                         INNER JOIN
                                      dbo.销售到款记录 
                         ON           dbo.产品销售合同.ID = dbo.销售到款记录.合同ID 
                         INNER JOIN
                                      dbo.产品订货单位 
                         ON           dbo.产品销售合同.订货单位ID = dbo.产品订货单位.ID";
            this.Message.Text = "·准备就绪";
            if (!this.IsPostBack)
            {
                this.SqlDataSource3.SelectCommand = sql;
                this.ViewState["SelectCommand"] = sql;
            }
            this.SqlDataSource3.SelectCommand = (string)this.ViewState["SelectCommand"];
            string[] dk = { "ID" };
            GridView2.DataKeyNames = dk;
            

        }
        //查询按钮处理逻辑
        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string temp = this.FaPiaoHao.Text.Trim().Replace("'", "");
            string Sql = "";
            Sql = "[产品销售合同].[合同编号] like '%" + temp + "%'";

            DateTime Dt;
            if (DateTime.TryParse(this.Date1.Text, out Dt))
            {
                Sql += " and [销售到款记录].[付款日期]>='" + Dt.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out Dt))
            {
                Sql += " and [销售到款记录].[付款日期]<='" + Dt.ToShortDateString() + "'";
            }
            if (this.GongHuoDanWei.Text != "0")
            {
                Sql += " and [产品订货单位].[名称]='" + this.GongHuoDanWei.SelectedItem.Text + "'";
            }
            this.SqlDataSource3.SelectCommand = @"
                         SELECT       dbo.销售到款记录.ID, 
                                      dbo.销售到款记录.收款单号, 
                                      dbo.产品销售合同.合同编号, 
                                      dbo.产品订货单位.名称, 
                                      dbo.产品销售合同.金额, 
                                      dbo.销售到款记录.收款, 
                                      dbo.销售到款记录.付款日期
                         FROM         dbo.产品销售合同 
                         INNER JOIN
                                      dbo.销售到款记录 
                         ON           dbo.产品销售合同.ID = dbo.销售到款记录.合同ID 
                         INNER JOIN
                                      dbo.产品订货单位 
                         ON           dbo.产品销售合同.订货单位ID = dbo.产品订货单位.ID 
                         WHERE " + Sql;

            this.ViewState["SelectCommand"] = this.SqlDataSource3.SelectCommand;


        }
        //分页按钮
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView2.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView2.PageIndex > 0) this.GridView2.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView2.PageIndex < this.GridView2.PageCount - 1) this.GridView2.PageIndex++;
                        break;
                    case "++":
                        this.GridView2.PageIndex = this.GridView2.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView2.PageCount)
                    {
                        this.GridView2.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView2.PageIndex + 1).ToString();
            }
        }
        //分页逻辑
        protected void GridView2_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView2.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView2.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView2.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView2.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView2.PageIndex == this.GridView2.PageCount - 1 || this.GridView2.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView2.PageIndex > this.GridView2.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }
        }
        //继续调用分页逻辑
        protected void GridView2_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView2_DataBound(null, null);
        }

        //新建按钮
        protected void Button_New_Click(object sender, EventArgs e)
        {
            Response.Redirect("XiaoShou_FuKuan_In.aspx");

        }


        //编辑按钮
        protected void Button_Edit_Click(object sender, EventArgs e)
        {
            if (GridView2.SelectedIndex >= 0 && GridView2.Rows.Count > 0)
            {
                Response.Redirect("XiaoShou_FuKuan_In.aspx?ID=" + GridView2.SelectedDataKey.Value.ToString());
            }
            else
            {
                this.Message.Text = "·请先选择一个项目";
            }
        }

        //删除按钮
        protected void Button_Delete_Click(object sender, EventArgs e)
        {
            if (GridView2.SelectedIndex >= 0 && GridView2.Rows.Count > 0)
            {
                this.SqlDataSource3.DeleteCommand = "Exec XiaoShou_FuKuan_Delete " + GridView2.SelectedDataKey.Value.ToString();
                this.SqlDataSource3.DeleteCommandType = SqlDataSourceCommandType.Text;
                this.SqlDataSource3.Delete();
                this.SqlDataSource3.SelectCommand = (string)this.ViewState["SelectCommand"];
                this.Message.Text = "·删除成功";
            }
            else
            {
                this.Message.Text = "·请先选择一个项目";
            }
        }

  
    }
}
